package com.lktx.sso.admin.service;

import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.core.ioc.annotation.Bean;
import cn.hserver.core.ioc.annotation.Value;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.RSA;
import com.lktx.sso.admin.entity.SsoAppJwtConfig;
import com.lktx.sso.admin.entity.SsoAppOidcConfig;
import com.lktx.sso.admin.mapper.SsoAppJwtConfigMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * jwt配置表 服务类
 *
 */
@Slf4j
@Bean
public class SsoAppJwtConfigService {

    @Autowired
    private SsoAppJwtConfigMapper mapper;
    @Value("oauth.baseIss")
    private String baseIss;
    /**
     * 分页查询列表
     *
     * @param page   分页对象
     * @param entity 查询实体
     * @return 所有数据
     */
    public Page<SsoAppJwtConfig> page(Page<SsoAppJwtConfig> page, SsoAppJwtConfig entity) {
        return mapper.paginate(page,new QueryWrapper()
                    .like(SsoAppJwtConfig::getRedirectUris, entity.getRedirectUris(), StrUtil.isNotEmpty(entity.getRedirectUris()))
                    .like(SsoAppJwtConfig::getPublicKey, entity.getPublicKey(), StrUtil.isNotEmpty(entity.getPublicKey()))
                    .like(SsoAppJwtConfig::getPrivateKey, entity.getPrivateKey(), StrUtil.isNotEmpty(entity.getPrivateKey()))
                    .like(SsoAppJwtConfig::getBindingType, entity.getBindingType(), StrUtil.isNotEmpty(entity.getBindingType()))
                .orderBy(SsoAppJwtConfig::getSsoAppId,false)
        );
    }

    /**
     * 不分页查询列表
     *
     * @param entity 实体对象
     * @return 查询结果
     */
    public List<SsoAppJwtConfig> list(SsoAppJwtConfig entity) {
        return mapper.selectListByQuery(new QueryWrapper()
                    .like(SsoAppJwtConfig::getRedirectUris, entity.getRedirectUris(), StrUtil.isNotEmpty(entity.getRedirectUris()))
                    .like(SsoAppJwtConfig::getPublicKey, entity.getPublicKey(), StrUtil.isNotEmpty(entity.getPublicKey()))
                    .like(SsoAppJwtConfig::getPrivateKey, entity.getPrivateKey(), StrUtil.isNotEmpty(entity.getPrivateKey()))
                    .like(SsoAppJwtConfig::getBindingType, entity.getBindingType(), StrUtil.isNotEmpty(entity.getBindingType()))
                .orderBy(SsoAppJwtConfig::getSsoAppId,false)
        );
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    public SsoAppJwtConfig getById(Integer id) {
        SsoAppJwtConfig ssoAppJwtConfig = mapper.selectOneById(id);
        ssoAppJwtConfig.setIss(baseIss);
        return ssoAppJwtConfig;
    }


    /**
     * 新增单条数据
     *
     * @param entity 实体对象
     */
    public void save(SsoAppJwtConfig entity) {
        SsoAppJwtConfig byId = getById(entity.getSsoAppId());
        if (byId != null) {
            if (entity.getPrivateKey() == null||entity.getPublicKey() == null) {
                RSA rsa = new RSA();
                entity.setPublicKey(rsa.getPublicKeyBase64());
                entity.setPrivateKey(rsa.getPrivateKeyBase64());
            }
            mapper.update(entity);
        }else {
            RSA rsa = new RSA();
            entity.setPublicKey(rsa.getPublicKeyBase64());
            entity.setPrivateKey(rsa.getPrivateKeyBase64());
            mapper.insert(entity);
        }
    }

    /**
     * 修改单条数据
     *
     * @param entity 实体对象
     */
    public void update(SsoAppJwtConfig entity) {
        mapper.update(entity);
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键集合
     */
    public void deleteByIds(List<Integer> ids) {
        mapper.deleteBatchByIds(ids);
    }

}